﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Willow.Authorization;

namespace Willow.MVC.HtmlHelper
{
    public static class CodeBlockExtension
    {

        public static MvcHtmlString CodeBlock(this System.Web.Mvc.HtmlHelper helper, string code, System.Func<Object, Object> html)
        {
            var function = html;
            var result = function.Invoke("").ToString();
            return CodeBlockExtension.CodeBlock(helper, code, result);
        }

        /// <summary>
        /// 根据权限输出代码
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="code">编码</param>
        /// <param name="html">Html代码</param>
        /// <returns></returns>
        public static MvcHtmlString CodeBlock(this System.Web.Mvc.HtmlHelper helper, string code, string html)
        {
            string htmlResult = "";
            string currUrl = helper.ViewContext.HttpContext.Request.Url.AbsolutePath;
            if (OnlineContext.Instance.GetCurrentUser().LoginActions.Any(p => p.BelongURL == currUrl && p.Handle == code && p.Category == ActionCategory.CodeBlock))
            {
                htmlResult = html;
            }
            return MvcHtmlString.Create(htmlResult);
        }
    }
}
